package com.igoal.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.igoal.dto.UserDTO;
import com.igoal.service.MailServiceImpl;
import com.igoal.service.SaveResult;
import com.igoal.service.UserAccountService;
import com.igoal.service.UserManagerExt;
import com.igoal.utils.RandomString;
import com.igoal.utils.StringUtils;
import com.igoal.web.model.BaseModel;

@Controller
@RequestMapping("/accountDisable.html")
public class AccountDisableController extends BaseController {

	private Log logger = LogFactory.getLog(AccountDisableController.class);
	private MailServiceImpl mailService;

	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setMailService(MailServiceImpl mailService) {
		this.mailService = mailService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showPage(ModelMap modelMap, HttpServletRequest request) {
		try {
			UserDTO user = getUserSession();
			if (user != null) {
				return "redirect:dashboard.html";
			}
		} catch (Exception ex) {
			logger.error("", ex);
		}

		String username = request.getParameter("username");
		String code = request.getParameter("code");

		UserDTO user = userManagerExt.getUserByUsername(username);
		if (user == null) {
			return "login";
		}

		if (StringUtils.isNotBlank(username)) {
			boolean isDisable = userManagerExt.isDisable(username);
			if (isDisable) {
				if (StringUtils.isNotBlank(code)) {
					if (userManagerExt.checkActivationCode(username, code)) {
						String newPassword = RandomString.generate(10);
						int result = userManagerExt.savePassword(user, newPassword);
						userManagerExt.setCredentialsExpired(user, false);
						if (result == SaveResult.SUCCESS) {
							mailService.sendPassword(user, newPassword);
							logger.debug("Create new password for " + user.getUsername()
									+ " successfully");
							modelMap.addAttribute("success", true);
						} else {
							logger.debug("Create new password for " + user.getUsername()
									+ " unsuccessfully");
							return "login";
						}
					}
				} else {
					return getViewPrefix() + "accountDisable";
				}
			}
		}

		return getViewPrefix() + "login";
	}

	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}

	public void initLeftPage(BaseModel model, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}
}
